home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 0957 / gnugrep / gui / dirmat~1.cpp < prev    next >
C/C++ Source or Header  |  1996-08-18  |  11KB  |  311 lines

  1. /***********************************************************************
  2. * class CDirMatcher - Assists in processing directories to find
  3. * files matching wildcard, attribute, date/time and filesize
  4. * requirements.
  5. *
  6. * Copyright (C) D. Munro 1996
  7. *
  8. * This program source is freely redistributable.
  9. ************************************************************************
  10.  
  11. #include "stdafx.h"
  12. #include "DirMatcher.h"
  13.  
  14. //#define _DEBUG_PRINT
  15.  
  16. void CDirMatcher::_SetDates_(COleDateTime *pStartTime, COleDateTime *pEndTime)
  17. //----------------------------------------------------------------------------
  18. {    if (pStartTime != NULL)
  19.         m_oledteStartTime = *pStartTime;
  20.     else
  21.         m_oledteStartTime = time_t(0);
  22.     if (pEndTime != NULL)
  23.         m_oledteEndTime = *pEndTime;
  24.     else
  25.         m_oledteEndTime = COleDateTime(9999,12,31,0,0,0);
  26. }
  27.  
  28. void CDirMatcher::_SetSizes_(DWORD dwMinSizeLo, DWORD dwMinSizeHi, 
  29.                                                          DWORD dwMaxSizeLo, DWORD dwMaxSizeHi)
  30. //----------------------------------------------------------------
  31. {    m_qwMinSize = (__int64)dwMinSizeLo | ((__int64)dwMinSizeHi << 32);
  32.     m_qwMaxSize = (__int64)dwMaxSizeLo | ((__int64)dwMaxSizeHi << 32);
  33. }
  34.  
  35. void CDirMatcher::_Constructor_(BOOL bMatchHidden, BOOL bMatchSystem, 
  36.                                                                 BOOL bMatchReadOnly, BOOL bMatchDirectory,
  37.                                                                 BOOL bMatchNormal, BOOL bMatchArchive,
  38.                                                                 COleDateTime *pStartTime, COleDateTime *pEndTime, 
  39.                                                                 DWORD dwMinSizeLo, DWORD dwMinSizeHi, 
  40.                                                                 DWORD dwMaxSizeLo, DWORD dwMaxSizeHi)
  41. //------------------------------------------------------------------------------
  42. {    _SetDates_(pStartTime, pEndTime);
  43.     _SetSizes_(dwMinSizeLo, dwMinSizeHi, dwMaxSizeLo, dwMaxSizeHi);
  44.     m_bMatchReadOnly = bMatchReadOnly;
  45.     m_bMatchDirectory = bMatchDirectory;
  46.     m_bMatchHidden = bMatchHidden; 
  47.     m_bMatchSystem = bMatchSystem;
  48.     m_bMatchNormal = bMatchNormal; 
  49.     m_bMatchArchive = bMatchArchive;
  50. }
  51.  
  52. CDirMatcher::CDirMatcher(CStringArray &strarrSpecs, 
  53.                                                     BOOL bMatchHidden, BOOL bMatchSystem, 
  54.                                                     BOOL bMatchReadOnly, BOOL bMatchDirectory,
  55.                                                     BOOL bMatchNormal, BOOL bMatchArchive,
  56.                                                     COleDateTime *pStartTime, COleDateTime *pEndTime, 
  57.                                                     DWORD dwMinSizeLo, DWORD dwMinSizeHi, 
  58.                                                     DWORD dwMaxSizeLo, DWORD dwMaxSizeHi)
  59. //-----------------------------------------------------------------------
  60. {    m_strarrFileSpecs.SetSize(strarrSpecs.GetSize(),10);
  61.     for (int i=0; i<strarrSpecs.GetSize(); i++)
  62.         m_strarrFileSpecs[i] = strarrSpecs[i];
  63.     _Constructor_(bMatchHidden, bMatchSystem, bMatchReadOnly, bMatchDirectory,  
  64.                                 bMatchNormal, bMatchArchive, pStartTime, pEndTime, 
  65.                                 dwMinSizeLo, dwMinSizeHi, dwMaxSizeLo, dwMaxSizeHi);
  66. }
  67.  
  68. // Takes a ; delimited string of specs eg *.c;*.cpp;*.h    
  69. CDirMatcher::CDirMatcher(CString strSpecs, 
  70.                                                     BOOL bMatchHidden, BOOL bMatchSystem, 
  71.                                                     BOOL bMatchReadOnly, BOOL bMatchDirectory,
  72.                                                     BOOL bMatchNormal, BOOL bMatchArchive,
  73.                                                     COleDateTime *pStartTime, COleDateTime *pEndTime, 
  74.                                                     DWORD dwMinSizeLo, DWORD dwMinSizeHi, 
  75.                                                     DWORD dwMaxSizeLo, DWORD dwMaxSizeHi)
  76. //-----------------------------------------------------------------------
  77. {    _ParseSpecs_(strSpecs);
  78.     _Constructor_(bMatchHidden, bMatchSystem, bMatchReadOnly, bMatchDirectory, 
  79.                                 bMatchNormal, bMatchArchive, pStartTime, pEndTime, 
  80.                                 dwMinSizeLo, dwMinSizeHi, dwMaxSizeLo, dwMaxSizeHi);
  81. }
  82.  
  83. void CDirMatcher::SetAttributes(BOOL bMatchReadOnly, BOOL bMatchDirectory,
  84.                                                                 BOOL bMatchHidden, BOOL bMatchSystem, 
  85.                                                                 BOOL bMatchNormal, BOOL bMatchArchive)
  86. //------------------------------------------------------------------------
  87. {    m_bMatchReadOnly = bMatchReadOnly;
  88.     m_bMatchDirectory = bMatchDirectory;
  89.     m_bMatchHidden = bMatchHidden; 
  90.     m_bMatchSystem = bMatchSystem;
  91.     m_bMatchNormal = bMatchNormal; 
  92.     m_bMatchArchive = bMatchArchive;
  93. }
  94.  
  95. void CDirMatcher::SetFileSpecs(CStringArray strarrSpecs) 
  96. //-------------------------------------------------------
  97. { m_strarrFileSpecs.RemoveAll();
  98.     m_strarrFileSpecs.SetSize(strarrSpecs.GetSize(),10);
  99.     for (int i=0; i<strarrSpecs.GetSize(); i++)
  100.         m_strarrFileSpecs[i] = strarrSpecs[i];
  101. }
  102.  
  103. void CDirMatcher::AddFileSpec(CString strSpec)
  104. //----------------------------------------
  105. {    int nIndex = m_strarrFileSpecs.GetSize();
  106.     m_strarrFileSpecs.SetAtGrow(nIndex,strSpec);
  107. }
  108.  
  109. int CDirMatcher::_ParseSpecs_(CString strSpecs)
  110. //---------------------------------------------
  111. {    m_strarrFileSpecs.RemoveAll();
  112.     m_strarrFileSpecs.SetSize(0,10);
  113.     CString strSpec;
  114.     int nSpecCnt =0;
  115.     strSpecs.TrimLeft(); strSpecs.TrimRight();
  116.     while (! strSpecs.IsEmpty())
  117.         {    int nSep = strSpecs.Find(';');
  118.             if (nSep == -1)
  119.                 nSep = strSpecs.GetLength();
  120.             strSpec = strSpecs.Left(nSep);
  121.             strSpecs = strSpecs.Mid(nSep+1);
  122.             strSpec.TrimLeft(); strSpec.TrimRight();
  123.             strSpecs.TrimLeft(); strSpecs.TrimRight();
  124.             if (! strSpec.IsEmpty())
  125.                 m_strarrFileSpecs.SetAtGrow(nSpecCnt++, strSpec);
  126.         }
  127.     return nSpecCnt;
  128. }
  129.  
  130. void CDirMatcher::_SetSpecs_(CString strSpec, CString &strName, CString &strExt)
  131. //------------------------------------------------------------------------------
  132. {    strSpec.TrimLeft(); strSpec.TrimRight();
  133.     ASSERT(! strSpec.IsEmpty());
  134.     int nDot = strSpec.ReverseFind('.');
  135.     if (nDot > -1)
  136.         {    strExt = strSpec.Mid(nDot+1);
  137.             strName = strSpec.Left(nDot);
  138.         }
  139.     else
  140.         {    strExt = "*";    // Match a* means same as Match a*.*
  141.             strName = strSpec;
  142.         }
  143.     strName.TrimLeft(); strName.TrimRight();
  144.     strExt.TrimLeft(); strExt.TrimRight();
  145. }
  146.  
  147. void CDirMatcher::_ExpandSpec_(CString &strExpNameSpec, int nNameLen)
  148. //-------------------------------------------------------------------
  149. {    BOOL bWildCard =FALSE;
  150.     int i;
  151.     for (i=strExpNameSpec.GetLength(); i<nNameLen; i++)
  152.         strExpNameSpec += (TCHAR)127;
  153. #ifdef _DEBUG_PRINT
  154.     TRACE1("Exp Len = %d\n",strExpNameSpec.GetLength());
  155. #endif
  156.     //ASSERT(strExpNameSpec.GetLength() == nNameLen);
  157.     for (i=0; i<nNameLen; i++)
  158.         {    TCHAR ch = strExpNameSpec[i];
  159.             if (ch == '*')
  160.                 bWildCard = TRUE;
  161.             else
  162.                 if (ch != '*' && ch != '?' && ch != (TCHAR)127)
  163.                     bWildCard = FALSE;
  164.                 else;
  165.             if (bWildCard)
  166.                 strExpNameSpec.SetAt(i,'?');
  167.             else
  168.                 strExpNameSpec.SetAt(i, ch);
  169.         }
  170.     for (i=0; i<strExpNameSpec.GetLength(); i++)
  171.         if (strExpNameSpec[i] == (TCHAR)127)
  172.             strExpNameSpec.SetAt(i,' ');    
  173. }
  174.  
  175. BOOL CDirMatcher::_MatchSpecToName_(CString strSpec, CString strName)
  176. //-------------------------------------------------------------------
  177. {    for (int i=0; i<strSpec.GetLength(); i++)
  178.         {    if (strSpec[i] == '?') continue;
  179.             if (i >= strName.GetLength()) return FALSE;
  180.             if (strSpec[i] == strName[i]) continue;
  181.             return FALSE;
  182.         }
  183.     return TRUE;
  184. }
  185.  
  186. BOOL CDirMatcher::Match(WIN32_FIND_DATA *pFindData)
  187. //-------------------------------------------------
  188. {    CString strFileName(pFindData->cFileName);
  189.     strFileName.MakeUpper();
  190.     CString strName, strExt;
  191.     COleDateTime oledteFileTime = COleDateTime(pFindData->ftLastWriteTime);
  192.     __int64 qwSize = (__int64)pFindData->nFileSizeLow | ((__int64)pFindData->nFileSizeHigh << 32);
  193.  
  194.     if (! m_bMatchHidden)
  195.         if (( pFindData->dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN)
  196.         {    
  197. #ifdef _DEBUG_PRINT
  198.                 TRACE1("Hidden File %s rejected\n",strFileName);
  199. #endif
  200.                 return FALSE;
  201.             }
  202.     if (! m_bMatchSystem)
  203.         if (( pFindData->dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) == FILE_ATTRIBUTE_SYSTEM)
  204.             {    
  205. #ifdef _DEBUG_PRINT
  206.                 TRACE1("System File %s rejected\n",strFileName);
  207. #endif
  208.                 return FALSE;
  209.             }
  210.     if (! m_bMatchReadOnly)
  211.         if (( pFindData->dwFileAttributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY)
  212.             {    
  213. #ifdef _DEBUG_PRINT            
  214.                 TRACE1("Read Only File %s rejected\n",strFileName);
  215. #endif
  216.                 return FALSE;
  217.             }
  218.     if (! m_bMatchDirectory)
  219.         if (( pFindData->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
  220.             {    
  221. #ifdef _DEBUG_PRINT            
  222.                 TRACE1("Directory %s rejected\n",strFileName);
  223. #endif
  224.                 return FALSE;
  225.             }
  226.     if (! m_bMatchNormal)
  227.         if (( pFindData->dwFileAttributes & FILE_ATTRIBUTE_NORMAL) == FILE_ATTRIBUTE_NORMAL)
  228.             {    
  229. #ifdef _DEBUG_PRINT            
  230.                 TRACE1("Normal File %s rejected\n",strFileName);
  231. #endif
  232.                 return FALSE;
  233.             }
  234.     if (! m_bMatchArchive)
  235.         if (( pFindData->dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) == FILE_ATTRIBUTE_ARCHIVE)
  236.             {    
  237. #ifdef _DEBUG_PRINT                
  238.                 TRACE1("Normal File %s rejected\n",strFileName);
  239. #endif
  240.                 return FALSE;
  241.             }
  242.     
  243.     if ( (oledteFileTime.GetStatus() == COleDateTime::valid) && (m_oledteStartTime.GetStatus() == COleDateTime::valid))
  244.         if ( (oledteFileTime >= m_oledteStartTime) && (oledteFileTime <= m_oledteEndTime) )
  245.             { 
  246. #ifdef _DEBUG_PRINT        
  247.             TRACE("Date/Time matches : %s in %s - %s\n",oledteFileTime.Format(),
  248.                             m_oledteStartTime.Format(), m_oledteEndTime.Format());
  249. #endif
  250.             }
  251.         else
  252.             {    
  253. #ifdef _DEBUG_PRINT        
  254.                 TRACE("Date/Time Fails : %s not in %s - %s\n",oledteFileTime.Format(),
  255.                             m_oledteStartTime.Format(), m_oledteEndTime.Format());
  256. #endif
  257.                 return FALSE;
  258.             }
  259.     if ( (qwSize >= m_qwMinSize) && (qwSize <= m_qwMaxSize) )
  260.         { 
  261. #ifdef _DEBUG_PRINT            
  262.             TRACE0("Size matches\n"); 
  263. #endif
  264.         }
  265.     else
  266.         {    
  267. #ifdef _DEBUG_PRINT        
  268.             TRACE("Date/Time Fails : %dl64 not in %dl64 - %dl64\n",qwSize, m_qwMinSize, 
  269.                         m_qwMaxSize);
  270. #endif
  271.             return FALSE;
  272.         }
  273.  
  274.     for (int i=0; i<m_strarrFileSpecs.GetSize(); i++)
  275.         {    CString strNameSpec, strExtSpec;
  276.             _SetSpecs_(m_strarrFileSpecs[i], strNameSpec, strExtSpec);
  277.             strNameSpec.MakeUpper(); strExtSpec.MakeUpper();
  278.             _SetSpecs_(strFileName, strName, strExt);
  279.             CString strExpNameSpec(strNameSpec), strExpExtSpec(strExtSpec);
  280.             _ExpandSpec_(strExpNameSpec, strName.GetLength());
  281.             _ExpandSpec_(strExpExtSpec, strExt.GetLength());
  282. #ifdef _DEBUG_PRINT
  283.             TRACE2("Exp Name = %s, Exp Ext. = %s\n",strExpNameSpec,strExpExtSpec);
  284. #endif
  285.             BOOL bMatch = _MatchSpecToName_(strExpNameSpec, strName);
  286.             if (! bMatch) 
  287.                 {    
  288. #ifdef _DEBUG_PRINT                
  289.                     TRACE2("Name match failed %s != %s \n",strExpNameSpec, strName);
  290. #endif
  291.                     continue;
  292.                 }
  293. #ifdef _DEBUG_PRINT
  294.             TRACE2("Name matches %s == %s \n",strExpNameSpec, strName);
  295. #endif
  296.             bMatch = _MatchSpecToName_(strExpExtSpec, strExt);
  297.             if (! bMatch) 
  298.                 {    
  299. #ifdef _DEBUG_PRINT                
  300.                     TRACE2("Extension match fails %s != %s \n",strExpExtSpec, strExt);
  301. #endif
  302.                     continue;
  303.                 }
  304. #ifdef _DEBUG_PRINT
  305.             TRACE2("Extension matches %s == %s \n",strExpExtSpec, strExt);
  306. #endif
  307.             return TRUE;
  308.         }
  309.     return FALSE;
  310. }
  311.